var $baseUrl = {
    fileServiceUrl: window.g.fileServiceUrl,
    baseUrl: window.g.baseUrl,
    jumpUrl: window.g.jumpUrl
}

function jumpArticle(item) {
    if (item.jumpType == 'URL' && item.url) {
        // 在当前窗口打开新链接
        window.open(item.url, '_blank');
    } else if (item.jumpType == 'ARTICLE') {
        // console.log(item)
        window.open(`./navigationList.html?articleId=${item.articleId}`, '_blank')
    } else {
        //console.log('无跳转')
    }
}

function handleClick() {
    window.open($baseUrl.jumpUrl)
}
//监听页面刷新
function beforeunload() {
    window.addEventListener('load', function (event) {
        sessionStorage.setItem('closeBtnRight', 0)
        sessionStorage.setItem('closeBtnLeft', 0)
    });
}
function setNavHoverColor(newTagList) {
    let color = sessionStorage.hoverColor;
    console.log(color, newTagList)
    if (newTagList && newTagList.length && color) {
        Array.from(newTagList).forEach((element, index) => {
            element.addEventListener("mouseover", function (event) {
                element.style.backgroundColor = color
            });
            element.addEventListener("mouseleave", function (event) {
                element.style.backgroundColor = 'rgba(0, 0, 0, 0)'
                let activeDom = document.getElementById('activeBtn');
                console.log(activeDom)
                if (activeDom) {
                    activeDom.style.backgroundColor = color
                }
            });
        })
    }
}

//下拉组件选中时触发事件
function selectChange(id) {
    var select = document.getElementById(id);
    var index = select.selectedIndex;
    var selectedValue = select.options[index].value;
    let rut = websiteLit.find(e => e.portalWebsiteId == selectedValue)
    console.log(selectedValue, rut)
    rut && window.open(rut.url, '_blank');
}
//监听滚动滚动
function distanceFromTop() {
    // 获取div元素
    const nav = document.getElementById('nav');
    const navPlaceholder = document.getElementById('navPlaceholder');
    // 监听滚动事件
    window.addEventListener('scroll', function () {
        // 计算nav距离顶部的距离
        const rect = nav.getBoundingClientRect();

        const distanceFromTop = rect.top;
        var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
        // 输出距离
        // console.log('距离顶部:', distanceFromTop, scrollTop);
        if (scrollTop <= 210) {
            nav.style.position = 'static';
            navPlaceholder.style.display = 'none'
        } else {
            nav.style.position = 'fixed';
            nav.style.top = 0;
            nav.style.left = 0;
            navPlaceholder.style.display = 'block'
        }
    });
}

function queryPortalWebsite() {
    ajax('GET', 'portalWebsite/homepage/queryPortalWebsite', null, function (res) {
        var { data } = res
        var websiteObj = {};
        websiteLit = data || [];
        websiteLit.forEach(item => {
            if (!websiteObj[item.portalWebsiteType]) {
                websiteObj[item.portalWebsiteType] = []
            }
            websiteObj[item.portalWebsiteType].push(item)
        })

        provinceOptions = websiteObj['PROVINCE'];
        cityOptions = websiteObj['CITY'];
        districtOptions = websiteObj['STRAIGHT'];
        otherOptions = websiteObj['OTHER'];
        initSelect('provinceOptions', otherOptions)
        initSelect('cityOptions', provinceOptions)
        initSelect('districtOptions', cityOptions)
        initSelect('otherOptions', districtOptions)
    })
};

//初始化下拉组件
function initSelect(id, list) {
    var select = document.getElementById(id);
    list.forEach((e, index) => {
        var newDiv = document.createElement('option');
        newDiv.value = e.portalWebsiteId;
        newDiv.className = 'option';
        newDiv.innerHTML = e.name
        select.appendChild(newDiv);
    })
}

